#include <iostream>

using namespace std;

#include "cgal_helper.h"
#include "ConvexHull.h"

ConvexHull::ConvexHull(vector<Point> p_list): p_list(p_list) {}

Polyhedron ConvexHull::subset_hull(vector<Point> p_sub_list) {
  Polyhedron poly;
  CGAL::convex_hull_3(p_sub_list.begin(), p_sub_list.end(), poly);
  return poly;
}

vector<vector<Point> > ConvexHull::run() {
  vector<Point> p_temp_list = p_list;
  vector<vector<Point> > hull_list;

  while (p_temp_list.size() > 4) {
    Polyhedron p  = subset_hull(p_temp_list);
    Polyhedron_Vertex_handle v_handle;
    vector<Point> hull_points;

    for (Polyhedron_Vertex_iterator it = p.vertices_begin(); it != p.vertices_end(); it++) {
      for (unsigned int i = 0; i < p_temp_list.size(); i++) {
        if (p_temp_list [i] == it->point()) {
          p_temp_list.erase(p_temp_list.begin() + i);
          hull_points.push_back(it->point());
          break;
        }
      }
    }
    hull_list.push_back(hull_points);
  }
  hull_list.push_back(p_temp_list);

  int size  = 0;
  for (vector<vector<Point> >::iterator it = hull_list.begin(); it != hull_list.end(); it++) {
    size  += it->size();
  }
  cout << "List size: " << size << " Actual size: " << p_list.size() << endl;
  return hull_list;
}
